<?php

namespace Apipad\Controller;
//分类管理 ：type:0-商品分类;1-工艺部件;2-标准数据库分类
header("content-type:application/json;charset=utf-8");
class MysqlCateController extends CommonController
{

       //左侧分类
    public function mysqls_cate(){
        $stype = I('stype');
        if($stype == ''){
            $data['code'] = 0;
            $data['msg'] = '分类类型不能为空';
            exit(json_encode($data));
        }
        $model = D('UserMysqlCate');
        $map = [];
        $map['uid'] = UID;
        $map['pid'] = 0;
        $map['stype'] = $stype;
        $map['isdel'] = 0;
        $list = $model->where($map)->order('add_time desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
    //列表
    public function index(){
        $detail = I('');
        $page=$detail['page']?$detail['page']:1;
        $limit=$detail['limit']?$detail['limit']:20;
        $kewords=$detail['kewords'];
        $pid = $detail['pid'];
        $stype = $detail['stype'];
        $model = D('UserMysqlCate');
        $map = [];
        if(I('keyword')){
            $map['title'] = array('like','%'.I('keyword').'%');
        }
        $map['uid'] = UID;
        $map['pid'] = $pid;
        $map['stype'] = $stype;
        $map['isdel'] = 0;
        $count = $model->where($map)->count();
        $list = $model->where($map)->order('add_time desc')->page($page,$limit)->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['count'] = $count;
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }
    //添加
    public function add(){
        $detail = I('');
        $pid = $detail['pid'];
        $title = $detail['title'];
        $stype = $detail['stype'];
        $sht_c= $detail['sht_c'];
        if(empty($title) || $title=="undefined"){
            $data['code'] = 0;
            $data['msg'] = '请填写分类名称';
            exit(json_encode($data));
        }
        $model = D('UserMysqlCate');
        $s_data['uid'] = UID;
        $s_data['title'] = $title;
        $s_data['pid'] = $pid;
        $s_data['stype'] = $stype;
        $s_data['sht_c'] = $sht_c;
        $s_data['add_time'] = time();
        $result = $model->add($s_data);
        if($result){
            $data['code'] = 200;
            $data['msg'] = '添加成功';
            $data['data']= $result;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '添加失败';
            exit(json_encode($data));
        }
    }
    //详情
    public function detail(){
        $detail = I('');
        $cate_id = $detail['cate_id'];
        $stype = $detail['stype'];
        if(empty($cate_id)){
            $data['code'] = 0;
            $data['msg'] = '分类ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserMysqlCate');
        $map['id'] = $cate_id;
        $map['isdel'] = 0;
        $map['uid'] = UID;
        $info = $model->where($map)->find();
        if($info){
            $data['code'] = 200;
            $data['msg'] = '查询成功';
            $data['data'] = $info;
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '数据不存在';
            exit(json_encode($data));
        }
    }
    //编辑
    public function edit(){
        $detail = I('');
        $cate_id = $detail['cate_id'];
        $title = $detail['title'];
        if(empty($cate_id)){
            $data['code'] = 0;
            $data['msg'] = '分类ID不能为空';
            exit(json_encode($data));
        }
        if(empty($title)){
            $data['code'] = 0;
            $data['msg'] = '分类名称不能为空';
            exit(json_encode($data));
        }
        $model = D('UserMysqlCate');
        $map['id'] = $cate_id;
        $map['uid'] = UID;
        $s_data['title'] = $title;
        $s_data['edit_time'] = time();
        $result = $model->where($map)->save($s_data);
        if($result){
            $data['code'] = 200;
            $data['msg'] = '编辑成功';
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '编辑失败';
            exit(json_encode($data));
        }
    }
    //删除
    public function del(){
        $detail = I('');
        $cate_id = $detail['cate_id'];
        if(empty($cate_id)){
            $data['code'] = 0;
            $data['msg'] = '分类ID不能为空';
            exit(json_encode($data));
        }
        $model = D('UserMysqlCate');
        $info = $model->where(['id'=>$cate_id,'uid'=>UID,'isdel'=>0])->find();
        $d_where = [];
        $d_where['uid'] = UID;
        $d_where['isdel']= 0;
        //0=商品分类；1=工艺部件,2=标准数据库；3=特殊体型
        if($info['stype'] == 0){
            //产品
            $d_model = D('UserGoods');
            $d_where['cate_id'] = $cate_id;
        }elseif($info['stype'] == 1){
            //工艺部件
            $d_model = D('user_technology');
            $d_where['cate_id'] = $cate_id;
        }elseif($info['stype'] == 2){
            if($info['sht_c'] == 0){
                //上装
                $d_model = M('user_mysql_list');
                $d_where['cat_id'] = $cate_id;
            }elseif($info['sht_c'] == 1){
                //下装
                $d_model = M('user_mysql_listc');
                $d_where['cat_id'] = $cate_id;
            }else{
                $data['code'] = 0;
                $data['msg'] = '参数错误';
                exit(json_encode($data));
            }
        }elseif($info['stype'] == 3){
            $d_model = M('user_tstx_pic');
            $d_where['cat_id'] = $cate_id;
        }else{
            $data['code'] = 0;
            $data['msg'] = '参数错误';
            exit(json_encode($data));
        }
        if($info['pid'] == 0){
            $child_list = $model->where(['pid'=>$cate_id,'isdel'=>0])->select();
            if($child_list){
                $data['code'] = 0;
                $data['msg'] = '该分类下存在子分类,无法删除';
                exit(json_encode($data));
            }
        }
        $d_info = $d_model->where($d_where)->find();
        if($d_info){
            $data['code'] = 0;
            $data['msg'] = '该分类存在关联数据,不能删除';
            exit(json_encode($data));
        }
        $map['id'] = $cate_id;
        $map['uid'] = UID;
        $s_data['isdel'] = 1;
        $s_data['del_time'] = time();
        $result = $model->where($map)->save($s_data);
        if($result){
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        }else{
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }
    public function index_gongyi(){
        $detail = I('post.');
        $keyword=$detail['keyword'];
        $limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
        $status= $detail['status']? : 0;
        $model = M('Imgcate');
        $where['is_delete']=0;
        $where['status']=$status;
        $where['pid']=0;
        if($keyword) {
            $where['title'] = array('like',"%".$keyword."%");
        }
        $count=$model->where($where)->count();
        $list= $model->where($where)->field('id,title,pid')->order('id asc')->page($page, $limit)->select();
        if($list){
            foreach($list as $k=>$val){
                $list[$k]['level']=1;
                $lis5c=$model->where(['pid'=>$val['id'],'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
                $list[$k]['counts']=count($lis5c);
                $list[$k]['child_list']=$lis5c;
                if($lis5c){
                       foreach($lis5c as $k1=>$v1){
                            $list[$k]['child_list'][$k1]['level']=2;
                            $list[$k]['child_list'][$k1]['counts']=count($this->get_child($v1['id']));
                            $list[$k]['child_list'][$k1]['children']=$this->get_child($v1['id']);
                       }
                }
            }
        }
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$list;
        exit(json_encode($data_cc));
    }
    //查询子分类
    function get_child($pid){
        $model = M('Imgcate');
        $lis5c=$model->where(['pid'=>$pid,'is_delete'=>0])->field('id,title,pid')->order('id desc')->select();
        return $lis5c;
    }
}
